﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace bookTools
{
    public partial class Form2 : Form
    {
        //XmlHelper xmlHelper;
        public Form2()
        {
            //xmlHelper = new XmlHelper();
            booklist = new List<Book>();
            chaplist = new List<Chap>();
            InitializeComponent();

        }
        
        private void book_add_Click(object sender, EventArgs e)
        {

        }

        private void book_del_Click(object sender, EventArgs e)
        {

        }
        string path;
        List<Book> booklist;
        List<Chap> chaplist;
        private void book_open_Click(object sender, EventArgs e)
        {
            try
            {

                if (this.folderBrowserDialog1.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                this.booklist.Clear();
                this.chaplist.Clear();
                this.path = this.folderBrowserDialog1.SelectedPath;
                if (!File.Exists(path + "/booklist.xml"))
                {
                    System.IO.File.Copy("booklist.xml", path + "/" + "booklist.xml", true);
                }

                //this.openFileDialog1.Filter = ".xml";
                //this.openFileDialog1.OpenFile();
                //path = this.openFileDialog1.FileName;
                XmlDocument dom = new XmlDocument();
                dom.Load(path + "/booklist.xml");//装载XML文档 
                //遍历所有节点 
                this.listBook.Items.Clear();
                
                int num = 0;
                booklist = new List<Book>();
                foreach (XmlElement book in dom.DocumentElement.ChildNodes)
                {
                    //读取数据 
                    Book aBook = new Book();

                    aBook.Title = book.SelectSingleNode("title").InnerText;
                    aBook.path = book.SelectSingleNode("path").InnerText;
                    aBook.disc = book.SelectSingleNode("desc").InnerText;

                    myListItem abookItem = new myListItem(aBook.Title, (Object)aBook);

                    booklist.Add(aBook);
                    this.listBook.Items.Add(abookItem);

                    if (!System.IO.Directory.Exists(path + "/" + aBook.path))
                    {
                        System.IO.Directory.CreateDirectory(path + "/" + aBook.path);
                        System.IO.File.Copy("chaplist.xml", path + "/" + aBook.path + "/" + "chaplist.xml", true);
                    }
                }
                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
               
            }

            
        }

        private void chap_add_Click(object sender, EventArgs e)
        {
            selectFlag = 1;
            this.groupBox3.Text = "chap";
        }

        private void chap_del_Click(object sender, EventArgs e)
        {

        }

        private void chap_save_Click(object sender, EventArgs e)
        {

        }

        string bookPath;
        String chapPath;
        int selectFlag = 0;
        
        private void listBook_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectFlag = 0;
            this.groupBox3.Text = "book";
            try
            {
                myListItem abookItem = (myListItem)this.listBook.SelectedItem;
                Book aBook = (Book)abookItem.Tag;

                this.textTitle.Text = aBook.Title;
                this.textdesc.Text = aBook.disc;
                this.textPath.Text = aBook.path;

                this.groupBox3.Text = "book:"+aBook.Title;

                bookPath = path + "/" + aBook.path;

                if (Directory.Exists(bookPath) == false)
                {
                    Directory.CreateDirectory(bookPath);
                    File.Copy("chaplist.xml", bookPath + "/chaplist.xml", true);
                }

                XmlDocument dom = new XmlDocument();
                dom.Load(bookPath + "/chaplist.xml");//装载XML文档 
                //遍历所有节点 
                this.listChap.Items.Clear();

                int num = 0;
                chaplist = new List<Chap>();
                foreach (XmlElement chap in dom.DocumentElement.ChildNodes)
                {
                    //读取数据 
                    Chap achap = new Chap();

                    achap.Title = chap.SelectSingleNode("title").InnerText;
                    achap.path = chap.SelectSingleNode("path").InnerText;
                    achap.disc = chap.SelectSingleNode("desc").InnerText;

                    myListItem achapItem = new myListItem(achap.Title, (Object)achap);

                    chaplist.Add(achap);
                    this.listChap.Items.Add(achapItem);

                    //if (!System.IO.Directory.Exists(path + "/" + aBook.path))
                    //{
                    //    System.IO.Directory.CreateDirectory(path + "/" + aBook.path);
                    //    System.IO.File.Copy("chaplist.xml", path + "/" + aBook.path + "/" + "chaplist.xml", true);
                    //}
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void listChap_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectFlag = 1;
            this.groupBox3.Text = "chap";
            try
            {
                myListItem achapItem = (myListItem)this.listChap.SelectedItem;
                Chap achap = (Chap)achapItem.Tag;

                this.textTitle.Text = achap.Title;
                this.textdesc.Text = achap.disc;
                this.textPath.Text = achap.path;

                String chapName = bookPath + "/" + achap.path;
                //this.richTextChap.LoadFile(chapName, RichTextBoxStreamType.RichText);
                FileStream fs = new FileStream(chapName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.UTF8);
                richTextChap.Text = sr.ReadToEnd();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void booklist2XML()
        {
            if (!File.Exists(path + "/booklist.xml"))
            {
                
            }
            else
            {
                File.Delete(path + "/booklist.xml");
            }
            System.IO.File.Copy(Application.StartupPath+"/booklist.xml", path + "/" + "booklist.xml", true);

            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(path + "/booklist.xml");//装载XML文档 

            XmlNode objRootNode = objXmlDoc.SelectSingleNode("booklist");

            foreach (Book abook in booklist)
            {
                XmlElement objChildNode = objXmlDoc.CreateElement("book");
                objRootNode.AppendChild(objChildNode);
                XmlElement objElement = objXmlDoc.CreateElement("title");
                objElement.InnerText = abook.Title;
                objChildNode.AppendChild(objElement);

                objElement = objXmlDoc.CreateElement("desc");
                objElement.InnerText = abook.disc;
                objChildNode.AppendChild(objElement);


                objElement = objXmlDoc.CreateElement("path");
                objElement.InnerText = abook.path;
                objChildNode.AppendChild(objElement);

            }
            objXmlDoc.Save(path + "/booklist.xml");

            this.listBook.Items.Clear();
            foreach (Book abook in booklist)
            {
                myListItem abookItem = new myListItem(abook.Title, (Object)abook);
                this.listBook.Items.Add(abookItem);
            }            
        }

        private void chaplist2XML()
        {
            if (!File.Exists(bookPath + "/chaplist.xml"))
            {

            }
            else
            {
                File.Delete(bookPath + "/chaplist.xml");
            }
            System.IO.File.Copy("chaplist.xml", bookPath + "/" + "chaplist.xml", true);

            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(bookPath + "/chaplist.xml");//装载XML文档 

            XmlNode objRootNode = objXmlDoc.SelectSingleNode("chaplist");
            int i = 0;
            foreach (Chap achap in chaplist)
            {
                XmlElement objChildNode = objXmlDoc.CreateElement("chap");
                objRootNode.AppendChild(objChildNode);

                XmlElement objElement = objXmlDoc.CreateElement("id");
                objElement.InnerText = i.ToString();
                i++;
                objChildNode.AppendChild(objElement);


                objElement = objXmlDoc.CreateElement("title");
                objElement.InnerText = achap.Title;
                objChildNode.AppendChild(objElement);

                objElement = objXmlDoc.CreateElement("desc");
                objElement.InnerText = achap.disc;
                objChildNode.AppendChild(objElement);


                objElement = objXmlDoc.CreateElement("path");
                objElement.InnerText = achap.path;
                objChildNode.AppendChild(objElement);

            }
            objXmlDoc.Save(bookPath + "/chaplist.xml");

            this.listChap.Items.Clear();
            foreach (Chap achap in chaplist)
            {
                myListItem achapItem = new myListItem(achap.Title, (Object)achap);
                this.listChap.Items.Add(achapItem);
            }
        }



        private void btnAdd_Click(object sender, EventArgs e)
        {
            if ( this.textTitle.Text.Trim() == "")
            {
                MessageBox.Show("Title 空");
                return;
            }

            if (this.textPath.Text.Trim() == "")
            {
                MessageBox.Show("path 空");
                return;
            }

            if (this.textdesc.Text.Trim() == "")
            {
                MessageBox.Show("desc 空");
                return;
            }

            if (selectFlag == 0)
            {
                Book abook = new Book();
                abook.Title = this.textTitle.Text;
                abook.path = this.textPath.Text;
                abook.disc = this.textdesc.Text;
                booklist.Add(abook);
                booklist2XML();

                if (Directory.Exists(path+"/"+abook.path) == false)
                {
                    Directory.CreateDirectory(path + "/" + abook.path);
                    File.Copy("chaplist.xml", path + "/" + abook.path + "/chaplist.xml", true);
                }                
            }
            else if (selectFlag == 1)
            {
                Chap aChap = new Chap();
                aChap.Title = this.textTitle.Text;
                aChap.disc = this.textdesc.Text;
                aChap.path = this.textPath.Text;
                chaplist.Add(aChap);

                chaplist2XML();
                File.Copy(this.textSrcpath.Text, bookPath  +"/" +this.textPath.Text, true);

            }
            else if (selectFlag == 2)
            {
                book2chap();
            }            
        }

        private void book2chap()
        {
            if (this.folderBrowserDialog1.ShowDialog() != DialogResult.OK)
            {

                return;
            }

            chaplist.Clear();

            this.textSrcpath.Text = this.folderBrowserDialog1.SelectedPath;

            DirectoryInfo theFolder = new DirectoryInfo(this.textSrcpath.Text);

            FileInfo[] fileInfo = theFolder.GetFiles();
            foreach (FileInfo NextFile in fileInfo)
            {
                String filename = NextFile.Name;
                if (filename.Substring(filename.LastIndexOf(".") + 1) == "txt")
                {
                    Chap aChap = new Chap();
                    aChap.Title = filename.Substring(filename.LastIndexOf("_")+1, filename.LastIndexOf(".") - filename.LastIndexOf("_"));
                    aChap.disc = aChap.Title;
                    aChap.path = filename;
                    chaplist.Add(aChap);
                    File.Copy(this.textSrcpath.Text+"/" + filename, bookPath + "/" + filename, true);
                }
            }

            chaplist2XML();
        }
        
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (selectFlag == 0)
            {
                Book abook = new Book();
                abook.Title = this.textTitle.Text;
                abook.path = this.textPath.Text;
                abook.disc = this.textdesc.Text;
                booklist.RemoveAt(this.listBook.SelectedIndex);
                booklist2XML();
            }
            else if (selectFlag == 1)
            {
                Chap aChap = new Chap();
                aChap.Title = this.textTitle.Text;
                aChap.disc = this.textdesc.Text;
                aChap.path = this.textPath.Text;
                chaplist.RemoveAt(this.listChap.SelectedIndex);
                chaplist2XML();
            }

        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            if (selectFlag == 0)
            {                
                booklist[this.listBook.SelectedIndex].Title = this.textTitle.Text;
                booklist[this.listBook.SelectedIndex].path = this.textPath.Text;
                booklist[this.listBook.SelectedIndex].disc = this.textdesc.Text;
                booklist2XML();
            }
            else if (selectFlag == 1)
            {
                chaplist[this.listChap.SelectedIndex].Title = this.textTitle.Text;
                chaplist[this.listChap.SelectedIndex].path = this.textPath.Text;
                chaplist[this.listChap.SelectedIndex].disc = this.textdesc.Text;             
                chaplist2XML();
            }
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textPath.Text = this.openFileDialog1.SafeFileName;
                this.textSrcpath.Text = this.openFileDialog1.FileName;
            }            
        }

        private void ToolBook_Click(object sender, EventArgs e)
        {
            selectFlag = 2;
            this.groupBox3.Text = "book-chap";
        }

    }

}
